#!/bin/bash

clear

echo
echo "Flash Sdrive Max Apple Mac batch file for Mac"
echo "by A.J. Huijts (Level42) - 2018"
echo "Version 1.0"
echo

VAR=$(ls /dev/tty.*)
vars=( $VAR )
Port=${vars[1]}

if [ "$Port" = "" ] ; then
	echo "Sdrive Max not found."
	echo "Please connect your Sdrive Max and re-run this batch file."
	echo
	exit
fi

echo "I think I found a Sdrive Max at this port : $Port"
echo
echo "Do you wish me to try to flash the Sdrive-Max ? (Y/N)"

read ANSWER

if [ "$ANSWER" = "y" ] ; then
	ANSWER="Y"
fi

if [ "$ANSWER" = "Y" ] ; then
	echo
	echo "OK, here we go !"
	echo
else
	echo
	echo "OK, I stopped as you wish."
	echo
	exit
fi

mydir="$(dirname "$BASH_SOURCE")"

avrdude -carduino -pm328p -P $Port -U flash:w:"$mydir/eeprom_writer.hex" /tmp

avrdude -carduino -pm328p -P $Port -U flash:w:"$mydir/SDrive.hex" /tmp

echo "Done !"